package com.servlet;

import com.DAO.GoodsDaoImpl;
import com.entity.Goods;
import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;


import java.io.IOException;

@WebServlet("/updateGoods")
public class UpdateGoodsServlet extends HttpServlet {
    private GoodsDaoImpl goodsDaoImpl = new GoodsDaoImpl();
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        
        try {
            Goods goods = new Goods();
            goods.setGid(Integer.parseInt(request.getParameter("gid")));
            goods.setGname(request.getParameter("gname"));
            goods.setGnum(Integer.parseInt(request.getParameter("gnum")));
            goods.setGprice(Double.parseDouble(request.getParameter("gprice")));
            goods.setGstatus("true".equals(request.getParameter("gstatus")));
            
            boolean success = goodsDaoImpl.updateGoods(goods);
            response.getWriter().write(new Gson().toJson(success));
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().write(new Gson().toJson(false));
        }
    }
} 